//
//  PutForwardRecordViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/16.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "PutForwardRecordViewController.h"
#import "PutFowardCell.h"
#import "PutForwardRecordStore.h"
#import "PutForwardRecordModel.h"
#import "PutForwardRecordSubModel.h"
@interface PutForwardRecordViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (weak, nonatomic) IBOutlet UILabel *currentTimeLa;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,copy) NSArray *allArr;
@property (nonatomic,copy) NSArray *listArr;
@property (nonatomic, copy) NSString *seletime;

@end

@implementation PutForwardRecordViewController

- (UIView *)headerView{
    
    if (!_headerView) {
        
        _headerView = [[[NSBundle mainBundle] loadNibNamed:@"PutForwardRecordHeaderView" owner:self options:nil] objectAtIndex:0];
    }
    
    return _headerView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"提现记录"];
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableView];
    [self getCurrentDate];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(45);
        make.left.right.top.equalTo(self.view).offset(0);
        
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headerView.mas_bottom).offset(1);
        make.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view);
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.estimatedRowHeight = 59;    
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.separatorColor = BASECOLOR;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    PutForwardRecordModel *model = self.listArr[section];
    
    NSArray *arr = [PutForwardRecordSubModel mj_objectArrayWithKeyValuesArray:model.data];
    
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PutForwardRecordModel *model = self.listArr[indexPath.section];
    
    NSArray *arr = [PutForwardRecordSubModel mj_objectArrayWithKeyValuesArray:model.data];
    
    PutForwardRecordSubModel *subModel = arr[indexPath.row];
    
    PutFowardCell *cell = [PutFowardCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = subModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 28;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    PutForwardRecordModel *model = self.listArr[section];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 28)];
    UILabel *la = [[UILabel alloc] init];
    la.font = [UIFont systemFontOfSize:14];
    NSString *mon = [NSString stringWithFormat:@"%@",model.mon];
    la.text = mon;
    [view addSubview:la];
    [la mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(14);
        make.top.bottom.mas_equalTo(0);
    }];
    
    
    UILabel *la2 = [[UILabel alloc] init];
    la2.textAlignment = NSTextAlignmentRight;
    la2.font = [UIFont systemFontOfSize:12];
    NSString *money = [NSString stringWithFormat:@"提现/购机总额：%@元",model.allmoney];
    la2.text = money;
    [view addSubview:la2];
    
    [la2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-14);
        make.centerY.equalTo(view);
    }];
    
    return view;
}

- (IBAction)onCalendar:(UIButton *)sender {
    
    NSDate *minDate = [NSDate setYear:1978 month:3 day:12];
    NSDate *maxDate = [NSDate date];
    
    MJWeakSelf
    [BRDatePickerView showDatePickerWithTitle:@"选择日期" dateType:BRDatePickerModeYM defaultSelValue:self.seletime minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
        
        NSArray *listArr = [selectValue componentsSeparatedByString:@"-"];
        weakSelf.currentTimeLa.text = [NSString stringWithFormat:@"%@年%@月",listArr[0],listArr[1]];
        weakSelf.seletime = selectValue;
        
    } cancelBlock:^{
        
        
    }];
}

- (void)refresh{
    
    PutForwardRecordStore *store = [[PutForwardRecordStore alloc] init];
    MJWeakSelf
    [store getForwardRecordWithCustomId:[UserDefaultsManager sharedInstance].user_id Success:^(NSArray *listArr){
        
        weakSelf.allArr = listArr;
        weakSelf.listArr = listArr;
        
        [weakSelf.tableView reloadData];
        
        [weakSelf.tableView.mj_header endRefreshing];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

#pragma mark - 查询
- (IBAction)onSearchBtn:(UIButton *)sender {
    
    if (self.currentTimeLa.text.length <= 0) {
        
        return;
    }
    
    NSRange range = NSMakeRange(5, 2);
    NSString *str = [NSString stringWithFormat:@"%@.%@",[self.currentTimeLa.text substringToIndex:4],[self.currentTimeLa.text substringWithRange:range]];
    
    
    NSArray *arr = [NSArray array];
    for (PutForwardRecordModel *model in self.allArr) {
        
        if ([model.mon isEqualToString:str]) {
            
            arr = @[model];
            
        }
    }
    
    [self.view makeToastActivity:CSToastPositionCenter];
    
    MJWeakSelf
    //GCD延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf.view hideToastActivity];
        weakSelf.listArr = arr;
        [weakSelf.tableView reloadData];
        
    });
    
    
}


- (void)getCurrentDate{
    
    NSDate *now = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy年MM月";
    NSString *str = [formatter stringFromDate:now];
    
    self.currentTimeLa.text = str;
    
}
@end
